package com.example.fund.util;

import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteStatement;

import com.example.fund.amplitude.Amplitude;
import com.example.fund.fundtrade.FundTradeInfo;
import com.example.fund.info.FundInfo;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;

public class FundTradeUtil {

    /**
     * 保存交易记录
     * @return
     */
    public static boolean saveFundTradeInfo(FundTradeInfo info)
    {
        if(info==null
                ||StringUtil.isNullOrEmpty(info.getAccount())
                ||StringUtil.isNullOrEmpty(info.getFundNumber())
                ||StringUtil.isNullOrEmpty(info.getDate())
                ||(info.getType()!=1 && info.getType()!=2 && info.getType()!=3)
                ||info.getTradePrice()<=0
                ||info.getTradeAmount()<=0
                ||info.getTradeQty()<=0)
        {
            return false;
        }

        if (info.getId()!=0)//修改
        {
            updateFundTradeInfo(info);
        }else                                      //新增
        {
            addFundTradeInfo(info);
        }

        return true;
    }

    /**
     * 新增交易记录
     * @param info
     * @return
     */
    public static String addFundTradeInfo(FundTradeInfo info)
    {
        SQLiteDatabase db = DBManager.getDB();

        StringBuffer insertSql = new StringBuffer();
        insertSql.append("	insert into fundTrade (	\n");
        insertSql.append("	    id	\n");
        insertSql.append("	    ,account	\n");
        insertSql.append("	    ,fundNumber	\n");
        insertSql.append("	    ,date	\n");
        insertSql.append("	    ,type	\n");
        insertSql.append("	    ,tradePrice	\n");
        insertSql.append("	    ,tradeAmount	\n");
        insertSql.append("	    ,tradeRate	\n");
        insertSql.append("	    ,tradeQty	\n");
        insertSql.append("	) values (	\n");
        insertSql.append("	    null	\n");
        insertSql.append("	    ,?	\n");
        insertSql.append("	    ,?	\n");
        insertSql.append("	    ,?	\n");
        insertSql.append("	    ,?	\n");
        insertSql.append("	    ,?	\n");
        insertSql.append("	    ,?	\n");
        insertSql.append("	    ,?	\n");
        insertSql.append("	    ,?	\n");
        insertSql.append("	)   	\n");

        db.beginTransaction();

        SQLiteStatement stmt = db.compileStatement(insertSql.toString());
        stmt.bindString(1, info.getAccount());
        stmt.bindString(2, info.getFundNumber());
        stmt.bindString(3, info.getDate());
        int type = info.getType();
        if (info.getType()==3)
        {
            type = 2;
        }
        stmt.bindLong(4, type);
        stmt.bindDouble(5,NumberUtil.setScale(info.getTradePrice(),6));
        stmt.bindDouble(6,NumberUtil.setScale(info.getTradeAmount(),2));
        stmt.bindDouble(7,NumberUtil.setScale(info.getTradeRate(),2));
        stmt.bindDouble(8,NumberUtil.setScale(info.getTradeQty(),6));
        stmt.execute();

        if (info.getType()==3)
        {
            double netValue = FundUtil.getFundNetValue(info.getTargetFundNumber(),info.getDate());
            if (netValue>0)
            {
                stmt.bindString(1, info.getAccount());
                stmt.bindString(2, info.getTargetFundNumber());
                stmt.bindString(3, info.getDate());
                stmt.bindLong(4, 1);
                stmt.bindDouble(5,NumberUtil.setScale(netValue,6));
                stmt.bindDouble(6,NumberUtil.setScale(info.getTradeAmount(),2));
                stmt.bindDouble(7,NumberUtil.setScale(0,2));
                double tradeQty = info.getTradeAmount()*(1-info.getTradeRate()/100.00d)/netValue;
                stmt.bindDouble(8,NumberUtil.setScale(tradeQty,6));
                stmt.execute();
            }
        }

        db.setTransactionSuccessful();
        db.endTransaction();

        return "";
    }

    /**
     * 新增交易记录
     * @param info
     * @return
     */
    public static String updateFundTradeInfo(FundTradeInfo info)
    {
        SQLiteDatabase db = DBManager.getDB();

        StringBuffer insertSql = new StringBuffer();
        insertSql.append("	update fundTrade 	\n");
        insertSql.append("	    set account=?	\n");
        insertSql.append("	    ,fundNumber=?	\n");
        insertSql.append("	    ,date=?	\n");
        insertSql.append("	    ,type=?	\n");
        insertSql.append("	    ,tradePrice=?	\n");
        insertSql.append("	    ,tradeAmount=?	\n");
        insertSql.append("	    ,tradeRate=?	\n");
        insertSql.append("	    ,tradeQty=?	\n");
        insertSql.append("	where id=? 	\n");

        db.beginTransaction();

        SQLiteStatement stmt = db.compileStatement(insertSql.toString());
        stmt.bindString(1, info.getAccount());
        stmt.bindString(2, info.getFundNumber());
        stmt.bindString(3, info.getDate());
        int type = info.getType();
        if (info.getType()==3)
        {
            type = 2;
        }
        stmt.bindLong(4, type);
        stmt.bindDouble(5,NumberUtil.setScale(info.getTradePrice(),6));
        stmt.bindDouble(6,NumberUtil.setScale(info.getTradeAmount(),2));
        stmt.bindDouble(7,NumberUtil.setScale(info.getTradeRate(),2));
        stmt.bindDouble(8,NumberUtil.setScale(info.getTradeQty(),6));
        stmt.bindLong(9, info.getId());
        stmt.execute();

        if (info.getType()==3)
        {
            double netValue = FundUtil.getFundNetValue(info.getTargetFundNumber(),info.getDate());
            if (netValue>0)
            {
                stmt.bindString(1, info.getAccount());
                stmt.bindString(2, info.getTargetFundNumber());
                stmt.bindString(3, info.getDate());
                stmt.bindLong(4, 1);
                stmt.bindDouble(5,NumberUtil.setScale(netValue,6));
                stmt.bindDouble(6,NumberUtil.setScale(info.getTradeAmount(),2));
                stmt.bindDouble(7,NumberUtil.setScale(0,2));
                double tradeQty = info.getTradeAmount()*(1-info.getTradeRate()/100.00d)/netValue;
                stmt.bindDouble(8,NumberUtil.setScale(netValue,6));
                stmt.execute();
            }
        }

        db.setTransactionSuccessful();
        db.endTransaction();

        return "";
    }

    /**
     * 删除交易记录
     * @return
     */
    public static boolean deleteFundTradeInfo(int id)
    {
        SQLiteDatabase db = DBManager.getDB();

        String delSql = "delete from fundTrade where id=?";
        SQLiteStatement stmt1 = db.compileStatement(delSql);
        stmt1.bindLong(1, id);
        stmt1.execute();

        return true;
    }

    /**
     * 查询全部账户
     * @return
     */
    public static List<String> getFundAccountList()
    {
        List<String> list = new LinkedList<String>();
        String sql = "select distinct account from fundTrade order by account ";
        SQLiteDatabase db = DBManager.getDB();
        Cursor c = db.rawQuery(sql.toString(), null);
        while (c.moveToNext())
        {
            String account = c.getString(c.getColumnIndex("account"));
            if (!StringUtil.isNullOrEmpty(account))
            {
                list.add(account);
            }
        }
        c.close();

        return list;
    }

    /**
     * 获取账户下的全部基金对象
     * @param account
     * @return
     */
    public static List<FundInfo> getFundInfoListByAccount(String account)
    {
        List<FundInfo> fundInfos = new LinkedList<FundInfo>();

        String sql = "select distinct fundNumber from fundTrade where account='"+account+"' ";
        SQLiteDatabase db = DBManager.getDB();
        Cursor c = db.rawQuery(sql.toString(), null);
        while (c.moveToNext())
        {
            String fundNumber = c.getString(c.getColumnIndex("fundNumber"));
            if (!StringUtil.isNullOrEmpty(fundNumber))
            {
                FundInfo info = FundUtil.getFundNetValueFromLocalhost("",fundNumber);
                info.setFundTradeQtyMap(getFundTradeQtyMap(account,fundNumber));
                fundInfos.add(info);
            }
        }
        c.close();

        return fundInfos;
    }

    /**
     * 查询交易记录
     * @return
     */
    public static List<FundTradeInfo> getFundTradeList(String fundAccountName, String fundNumber, String fundTypeNumber) {
        List<FundTradeInfo> retList = new LinkedList<FundTradeInfo>();

        if (StringUtil.isNullOrEmpty(fundAccountName))
        {
            return retList;
        }

        SQLiteDatabase db = DBManager.getDB();

        String date = FundUtil.getFundLastDate("0000001");

        StringBuffer sql = new StringBuffer();
        sql.append("	select 			");
        sql.append("	    tt.account			");
        sql.append("	    ,tt.fundNumber			");
        sql.append("	    ,ifnull(t1.name,t2.name) fundName			");
        sql.append("	    ,tt.costAmountTotal			");
        sql.append("	    ,tt.redeemAmountTotal			");
        sql.append("	    ,tt.tradeQtyTotal			");
        sql.append("	    ,t3.netValue tradePrice 	");
        sql.append("	    ,(case when tt.tradeQtyTotal<1 then 0 else tt.tradeQtyTotal end)*ifnull(t3.netValue,0) valueTotal			");
        sql.append("	from			");
        sql.append("	(			");
        sql.append("	select 			");
        sql.append("	    t.account			");
        sql.append("	    ,t.fundNumber			");
        sql.append("	    ,sum(case when t.type=1 then t.tradeAmount else 0 end) costAmountTotal			");
        sql.append("	    ,sum(case when t.type=2 then t.tradeAmount else 0 end) redeemAmountTotal			");
        sql.append("	    ,sum(case when t.type=1 then t.tradeQty else -t.tradeQty end) tradeQtyTotal			");
        sql.append("	from fundTrade t			");
        sql.append("	group by t.account,t.fundNumber			");
        sql.append("	) tt			");
        sql.append("	left join Fund t1 on tt.fundNumber=t1.number			");
        sql.append("	left join indexset t2 on tt.fundNumber=t2.number			");
        sql.append("	left join FundNetValue t3 on tt.fundNumber=t3.fundNumber and t3.date='"+date+"'			");
        sql.append("	where tt.account='"+fundAccountName+"' 	");
        if (!StringUtil.isNullOrEmpty(fundNumber))
        {
            sql.append("	and tt.fundNumber='"+fundNumber+"' 	");
        }
        sql.append("	order by valueTotal desc			");

        double costAmountSum = 0;
        double redeemAmountSum = 0;
        double valueSum = 0;
        Cursor c = db.rawQuery(sql.toString(), null);
        while (c.moveToNext())
        {
            String account = c.getString(c.getColumnIndex("account"));
            String fundNumber1 = c.getString(c.getColumnIndex("fundNumber"));
            String fundName = c.getString(c.getColumnIndex("fundName"));
            double tradePrice = c.getDouble(c.getColumnIndex("tradePrice"));
            double costAmountTotal = c.getDouble(c.getColumnIndex("costAmountTotal"));
            double redeemAmountTotal = c.getDouble(c.getColumnIndex("redeemAmountTotal"));
            double tradeQtyTotal = c.getDouble(c.getColumnIndex("tradeQtyTotal"));
            double valueTotal = c.getDouble(c.getColumnIndex("valueTotal"));

            FundTradeInfo info = new FundTradeInfo();
            info.setTotalType(2);
            info.setFundNumber(fundNumber1);
            info.setFundName(fundName+" 小计");
            info.setDate(date);
            info.setTradePrice(tradePrice);
            info.setAccount(account);
            info.setCostAmountTotal(costAmountTotal);
            info.setRedeemAmountTotal(redeemAmountTotal);
            info.setTradeQtyTotal(tradeQtyTotal);
            info.setValueTotal(valueTotal);

            retList.add(info);

            costAmountSum+=costAmountTotal;
            redeemAmountSum+=redeemAmountTotal;
            valueSum+=valueTotal;
        }
        c.close();

        sql.setLength(0);
        sql.append("select \n");
        sql.append("	t.id 	\n");
        sql.append("	,t.fundNumber 	\n");
        sql.append("	,ifnull(t1.name,t2.name) fundName 	\n");
        sql.append("	,t.account 	\n");
        sql.append("	,t.tradePrice 	\n");
        sql.append("	,t.tradeAmount 	\n");
        sql.append("	,t.tradeRate 	\n");
        sql.append("	,t.tradeQty 	\n");
        sql.append("	,t.date 	\n");
        sql.append("	,t.type 	\n");
        sql.append("from fundTrade t 	\n");
        sql.append("left join Fund t1 on t.fundNumber=t1.number 	\n");
        sql.append("left join indexset t2 on t.fundNumber=t2.number 	\n");
        sql.append("where t.account='"+fundAccountName+"' 	\n");
        sql.append("order by t.date desc,t.type 	\n");


        List<FundTradeInfo> infoList = new LinkedList<FundTradeInfo>();
        c = db.rawQuery(sql.toString(), null);
        while (c.moveToNext())
        {
            int id = c.getInt(c.getColumnIndex("id"));
            String fundNumber1 = c.getString(c.getColumnIndex("fundNumber"));
            String fundName = c.getString(c.getColumnIndex("fundName"));
            String date1 = c.getString(c.getColumnIndex("date"));
            String account = c.getString(c.getColumnIndex("account"));
            double tradePrice = c.getDouble(c.getColumnIndex("tradePrice"));
            double tradeAmount = c.getDouble(c.getColumnIndex("tradeAmount"));
            double tradeRate = c.getDouble(c.getColumnIndex("tradeRate"));
            double tradeQty = c.getDouble(c.getColumnIndex("tradeQty"));
            int type = c.getInt(c.getColumnIndex("type"));

            FundTradeInfo info = new FundTradeInfo();
            info.setId(id);
            info.setFundNumber(fundNumber1);
            info.setFundName(fundName);
            info.setDate(date1);
            info.setAccount(account);
            info.setTradeAmount(tradeAmount);
            info.setTradePrice(tradePrice);
            info.setTradeRate(tradeRate);
            info.setTradeQty(tradeQty);
            info.setType(type);

            infoList.add(info);
        }
        c.close();

        //总计
        FundTradeInfo sumInfo = new FundTradeInfo();
        sumInfo.setTotalType(1);
        sumInfo.setAccount(fundAccountName);
        sumInfo.setFundNumber("");
        sumInfo.setFundName("");
        sumInfo.setDate(date);
        sumInfo.setCostAmountTotal(costAmountSum);
        sumInfo.setRedeemAmountTotal(redeemAmountSum);
        sumInfo.setValueTotal(valueSum);
        retList.add(0,sumInfo);

        //计算各基金的累积申购金额、累积赎回金额、累积份额、累积市值
        Map<String,Double> costAmountTotalMap = new HashMap<String, Double>();
        Map<String,Double> redeemAmountTotalMap = new HashMap<String, Double>();
        Map<String,Double> tradeQtyTotalMap = new HashMap<String, Double>();
        double redeemBalanceAmountTotal = 0;//赎回时：赎回金额余额+=赎回金额；申购时：赎回金额余额-=max(0,赎回金额余额-申购金额)
        double investAmountTotal = 0;//赎回时：总投入金额不变；申购时：总投入金额=总投入金额+max(0,申购金额-赎回金额余额)
        for (int i=infoList.size()-1;i>=0;i--)
        {
            FundTradeInfo info = infoList.get(i);
            String fundNumber1 = info.getFundNumber();
            int type = info.getType();
            double tradeAmount = info.getTradeAmount();
            double tradeQty = info.getTradeQty();
            double tradePrice = info.getTradePrice();

            Double costAmountTotal = costAmountTotalMap.get(fundNumber1);
            if (costAmountTotal==null)
            {
                costAmountTotal = 0.0;
            }
            Double redeemAmountTotal = redeemAmountTotalMap.get(fundNumber1);
            if (redeemAmountTotal==null)
            {
                redeemAmountTotal = 0.0;
            }
            Double tradeQtyTotal = tradeQtyTotalMap.get(fundNumber1);
            if (tradeQtyTotal==null)
            {
                tradeQtyTotal = 0.0;
            }
            if (type==1)//申购
            {
                costAmountTotal+=tradeAmount;
                tradeQtyTotal = tradeQtyTotal+tradeQty;
                investAmountTotal += Math.max(0,tradeAmount-redeemBalanceAmountTotal);
                redeemBalanceAmountTotal = Math.max(0,redeemBalanceAmountTotal-tradeAmount);
            }else if (type==2)//购回
            {
                redeemAmountTotal += tradeAmount;
                tradeQtyTotal = tradeQtyTotal-tradeQty;
                redeemBalanceAmountTotal += tradeAmount;
            }
            if (tradeQtyTotal<1)
            {
                tradeQtyTotal = 0.0;
            }
            info.setCostAmountTotal(costAmountTotal);
            info.setRedeemAmountTotal(redeemAmountTotal);
            info.setTradeQtyTotal(tradeQtyTotal);
            info.setValueTotal(tradeQtyTotal*tradePrice);

            costAmountTotalMap.put(fundNumber1,costAmountTotal);
            redeemAmountTotalMap.put(fundNumber1,redeemAmountTotal);
            tradeQtyTotalMap.put(fundNumber1,tradeQtyTotal);
        }
        sumInfo.setInvestAmountTotal(investAmountTotal);

        for (int i=0;i<infoList.size();i++) {
            FundTradeInfo info = infoList.get(i);
            String fundNumber1 = info.getFundNumber();
            int type = info.getType();

            if (!StringUtil.isNullOrEmpty(fundNumber) && !fundNumber.equals(fundNumber1))
            {
                continue;
            }
            if (!StringUtil.isNullOrEmpty(fundTypeNumber) && !String.valueOf(type).equals(fundTypeNumber))
            {
                continue;
            }

            retList.add(info);
        }

        return retList;
    }

    /**
     * 查询基金的交易份额，交易日期-交易份额
     * @return
     */
    public static Map<String,Double> getFundTradeQtyMap(String fundAccountName, String fundNumber) {
        Map<String,Double> map = new HashMap<String,Double>();
        SQLiteDatabase db = DBManager.getDB();

        StringBuffer sql = new StringBuffer();

        sql.append("select \n");
        sql.append("	date 	\n");
        sql.append("	,sum(case when type=1 then tradeQty when type=2 then 0-tradeQty else 0 end) tradeQty 	\n");
        sql.append("from fundTrade t 	\n");
        sql.append("where 1=1 	\n");
        sql.append("and account='"+fundAccountName+"' 	\n");
        sql.append("and fundNumber='"+fundNumber+"' 	\n");
        sql.append("group by date  	\n");

        Map<String,List<FundTradeInfo>> infoMap = new HashMap<String, List<FundTradeInfo>>();
        Cursor c = db.rawQuery(sql.toString(), null);
        while (c.moveToNext())
        {
            String date = c.getString(c.getColumnIndex("date"));
            double tradeQty = c.getDouble(c.getColumnIndex("tradeQty"));
            if (!StringUtil.isNullOrEmpty(date))
            {
                map.put(date,tradeQty);
            }
        }
        c.close();

        return map;
    }

    /**
     * 查询交易记录基金名称
     * @return
     */
    public static List<String> getTradeFundNameList() {
        List<String> list = new LinkedList<String>();

        StringBuffer sql = new StringBuffer();
        sql.append("select distinct \n");
        sql.append("	ifnull(t1.name,t2.name) fundName 	\n");
        sql.append("from fundTrade t 	\n");
        sql.append("left join Fund t1 on t.fundNumber=t1.number 	\n");
        sql.append("left join indexset t2 on t.fundNumber=t2.number 	\n");
        sql.append("order by t.date desc 	\n");
        SQLiteDatabase db = DBManager.getDB();

        Cursor c = db.rawQuery(sql.toString(), null);
        while (c.moveToNext())
        {
            String fundName = c.getString(c.getColumnIndex("fundName"));
            if (!StringUtil.isNullOrEmpty(fundName))
            {
                list.add(fundName);
            }
        }
        c.close();

        return list;
    }

    /**
     * 查询累积份额
     * @param fundAccountName
     * @param fundNumber
     * @param date
     * @return
     */
    public static double getTradeQtyTotal(String fundAccountName, String fundNumber, String date)
    {
        double qty = 0;
        if (StringUtil.isNullOrEmpty(fundAccountName)
                ||StringUtil.isNullOrEmpty(fundNumber)
                ||StringUtil.isNullOrEmpty(date))
        {
            return qty;
        }

        SQLiteDatabase db = DBManager.getDB();

        StringBuffer sql = new StringBuffer();
        sql.append("	select 			");
        sql.append("	    sum(case when type=1 then tradeQty else -tradeQty end) tradeQtyTotal			");
        sql.append("	from fundTrade			");
        sql.append("	where account='"+fundAccountName+"' 	\n");
        sql.append("	and fundNumber='"+fundNumber+"' 	\n");
        sql.append("	and date<='"+date+"' 	\n");

        Cursor c = db.rawQuery(sql.toString(), null);
        if (c.moveToNext())
        {
            qty = c.getDouble(c.getColumnIndex("tradeQtyTotal"));
        }
        c.close();

        return qty;
    }

    /**
     * 批量新增交易记录
     */
    public static void addBatchTrade(String fundAccountName,String fund1Number,String fund2Number,double amount,String tradeDate,double fluctuateRate)
    {
        if(StringUtil.isNullOrEmpty(fundAccountName)
                ||StringUtil.isNullOrEmpty(fund1Number)
                ||StringUtil.isNullOrEmpty(fund2Number)
                ||StringUtil.isNullOrEmpty(tradeDate)
                ||amount<=0||fluctuateRate<=0)
        {
            return;
        }

        List<String> dateList = FundUtil.getSHIndexDateList();
        FundInfo fund1 = FundUtil.getFundNetValueFromLocalhost("",fund1Number);
        FundInfo fund2 = FundUtil.getFundNetValueFromLocalhost("",fund2Number);

        FundInfo cloneInfo = new FundInfo();
        cloneInfo.setName(fund2.getName());
        cloneInfo.setNumber(fund2.getNumber());
        Map<String,String> actValues = cloneInfo.getActualValues();
        Map<String,String> actValues1 = fund1.getActualValues();
        Map<String,String> actValues2 = fund2.getActualValues();
        String startTradeDate = "";

        for(int i=dateList.size()-1;i>=0;i--)
        {
            String dateStr = dateList.get(i);

            String actValue1 = actValues1.get(dateStr);
            String actValue2 = actValues2.get(dateStr);
            if(actValue1!=null && actValue2!=null)
            {
                double actValue = Double.valueOf(actValue2)/Double.valueOf(actValue1);
                actValues.put(dateStr, String.valueOf(actValue));

                if (StringUtil.isNullOrEmpty(startTradeDate) && dateStr.compareTo(tradeDate)>=0)
                {
                    startTradeDate = dateStr;
                }
            }
        }

        List<String> amplitudeDateList = new Amplitude().getAmplitudeDateList(cloneInfo, dateList, fluctuateRate, -fluctuateRate);

        if (amplitudeDateList.size()<=2 || StringUtil.isNullOrEmpty(startTradeDate))
        {
            return;
        }


        //删除同名账户数据
        deleteAccount(fundAccountName);

        //先买入A
        purchaseFund(fundAccountName,fund1Number,startTradeDate,amount);

        String lastTradeDate = startTradeDate;
        int j = dateList.size()-1;
        for(int i=2,s=amplitudeDateList.size();i<s;i++)
        {
            String dateStr = amplitudeDateList.get(i);

            if (startTradeDate.compareTo(dateStr)>=0)
            {
                continue;
            }

            String v = actValues.get(dateStr);

            for (;j>=0;j--)
            {
                String temDate = dateList.get(j);
                if (temDate.compareTo(dateStr)>0)
                {
                    String v1 = actValues.get(temDate);

                    if (StringUtil.isNullOrEmpty(v1))
                    {
                        continue;
                    }
                    double rise = 1+((Double.valueOf(v1)-Double.valueOf(v))/Double.valueOf(v));

                    if (rise<=1-(fluctuateRate/100.00d))
                    {
                        //赎回A
                        double temAmount = redeemFund(fundAccountName,fund1Number,temDate);
                        //申购B
                        purchaseFund(fundAccountName,fund2Number,temDate,temAmount);
                        break;
                    }

                    if (rise>=1.0d/(1-fluctuateRate/100.00d))
                    {
                        //赎回B
                        double temAmount = redeemFund(fundAccountName,fund2Number,temDate);
                        //申购A
                        purchaseFund(fundAccountName,fund1Number,temDate,temAmount);
                        break;
                    }
                }
            }
        }

    }

    /**
     * 申购
     * @param fundAccountName
     * @param fundNumber
     * @param date
     * @return
     */
    public static double purchaseFund(String fundAccountName, String fundNumber, String date,double amount)
    {
        double qty = 0;
        double v = FundUtil.getFundNetValue(fundNumber,date);
        if (v>0)
        {
            FundTradeInfo info = new FundTradeInfo();
            info.setAccount(fundAccountName);
            info.setFundNumber(fundNumber);
            info.setDate(date);
            info.setType(1);
            info.setTradePrice(v);
            info.setTradeAmount(amount);
            info.setTradeRate(0);
            info.setTradeQty(amount/v);

            saveFundTradeInfo(info);
        }

        return  qty;
    }

    /**
     * 赎回
     * @param fundAccountName
     * @param fundNumber
     * @param date
     * @return
     */
    public static double redeemFund(String fundAccountName, String fundNumber, String date)
    {
        double qty = getTradeQtyTotal(fundAccountName,fundNumber,date);
        double v = FundUtil.getFundNetValue(fundNumber,date);

        double amount = qty*v;

        FundTradeInfo info = new FundTradeInfo();
        info.setAccount(fundAccountName);
        info.setFundNumber(fundNumber);
        info.setDate(date);
        info.setType(2);
        info.setTradePrice(v);
        info.setTradeAmount(amount);
        info.setTradeRate(0);
        info.setTradeQty(qty);

        saveFundTradeInfo(info);
        return  amount;
    }

    /**
     * 删除账户
     * @param fundAccountName
     */
    public static void deleteAccount(String fundAccountName)
    {
        String sql = "delete from fundTrade where account='"+fundAccountName+"'";
        SQLiteDatabase db = DBManager.getDB();
        db.execSQL(sql);
    }
}
